/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.thinkgem.jeesite.modules.order.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.order.entity.FeiPaiDan;
import com.thinkgem.jeesite.modules.order.service.FeiPaiDanService;

/**
 * 货单分派Controller
 * @author 李忠
 * @version 2013-07-31
 */
@Controller
@RequestMapping(value = "${adminPath}/order/feiPaiDan")
public class FeiPaiDanController extends BaseController {

	@Autowired
	private FeiPaiDanService feiPaiDanService;
	
	@ModelAttribute
	public FeiPaiDan get(@RequestParam(required=false) Long id) {
		if (id != null){
			return feiPaiDanService.get(id);
		}else{
			return new FeiPaiDan();
		}
	}
	
	@RequiresPermissions("order:feiPaiDan:view")
	@RequestMapping(value = {"list", ""})
	public String list(FeiPaiDan feiPaiDan, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			feiPaiDan.setCreateBy(user);
		}
        Page<FeiPaiDan> page = feiPaiDanService.find(new Page<FeiPaiDan>(request, response), feiPaiDan); 
        model.addAttribute("page", page);
		return "modules/order/feiPaiDanList";
	}

	@RequiresPermissions("order:feiPaiDan:view")
	@RequestMapping(value = "form")
	public String form(FeiPaiDan feiPaiDan, Model model) {
		model.addAttribute("feiPaiDan", feiPaiDan);
		return "modules/order/feiPaiDanForm";
	}

	@RequiresPermissions("order:feiPaiDan:edit")
	@RequestMapping(value = "save")
	public String save(FeiPaiDan feiPaiDan, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, feiPaiDan)){
			return form(feiPaiDan, model);
		}
		feiPaiDanService.save(feiPaiDan);
		addMessage(redirectAttributes, "保存货单分派'" + feiPaiDan.getName() + "'成功");
		return "redirect:"+Global.getAdminPath()+"/modules/order/feiPaiDan/?repage";
	}
	
	@RequiresPermissions("order:feiPaiDan:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		feiPaiDanService.delete(id);
		addMessage(redirectAttributes, "删除货单分派成功");
		return "redirect:"+Global.getAdminPath()+"/modules/order/feiPaiDan/?repage";
	}

}
